package com.yzg.cas;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @author misterWei
 * @create 2020年05月24号:20点52分
 * @mailbox forcontinue@163.com
 */
@Slf4j(topic = "c.CasField")
public class CasField {

    public static void main(String[] args) {
        Student student = new Student();
        AtomicReferenceFieldUpdater<Student,String> a =
                AtomicReferenceFieldUpdater.newUpdater(Student.class,String.class,"name");
        String name = a.get(student);
        a.compareAndSet(student,name,"张三");
        System.out.println(student);
    }

    static class Student{
     volatile String name;
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
}
